package com.rt.easyjava.base.biz1.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rt.easyjava.base.biz1.bean.TmptBaseFileBiz;
import com.rt.easyjava.base.biz1.dto.TmptBaseFileBizDto;
import com.rt.easyjava.base.biz1.service.intf.TmptBaseFileBizService;
import com.rt.tmpt.service.utils.SpringContextHolder;
import com.rt.easyjava.common.base.MyQueryWrapper;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.base.UserDetailsDto;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.easyjava.common.exception.ControllerException;
import com.rt.easyjava.common.utils.others.DateTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

import java.lang.reflect.Method;

/**
 * 前端控制器
 * @author wuxiaoyong
 * @since 2020-11-20
 * 用户信息可直接形参获取：String (_userId，_loginName，_userName，_companyId，_userJson) or userDetailsDto.getUserId()
 */
@Slf4j
public class TmptBaseFileController extends ControllerException {
    @Autowired
    protected MyMapper mapper;
    @Autowired
    protected TmptBaseFileBizService tmptBaseFileBizService;


    protected Result page(TmptBaseFileBizDto tmptBaseFileBizDto, UserDetailsDto userDetailsDto){
        Result<TmptBaseFileBizDto> result = new Result<>();

        Page<TmptBaseFileBiz> page = new Page<>(tmptBaseFileBizDto.getPage(), tmptBaseFileBizDto.getLimit());

        // 查询条件
        TmptBaseFileBiz tmptBaseFileBizQuery = mapper.map(tmptBaseFileBizDto, TmptBaseFileBiz.class);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        tmptBaseFileBizQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        tmptBaseFileBizQuery.rebuildDataLimitQueryBean(userDetailsDto);

        QueryWrapper<TmptBaseFileBiz> queryWrapper=new QueryWrapper<>(tmptBaseFileBizQuery);
        //特殊查询条件
        queryWrapper.ge(tmptBaseFileBizDto.getCreatedate_Start()!=null,"createdate",tmptBaseFileBizDto.getCreatedate_Start());
        queryWrapper.le(tmptBaseFileBizDto.getCreatedate_End()!=null,"createdate",tmptBaseFileBizDto.getCreatedate_End());
        queryWrapper.orderBy(tmptBaseFileBizDto.getCondition(), tmptBaseFileBizDto.isAsc(), tmptBaseFileBizDto.getColumn());

        return tmptBaseFileBizService.page(page, queryWrapper);
    }

    /**
        树结构查询方法，非树结构会造一个空方法备用着。
    */
    protected Result tree (Long id) {
        Result<TmptBaseFileBizDto> result = new Result<>();
        // when id=0 , id=0 is parantId=0 , 默认设置一个空根
        TmptBaseFileBizDto tmptBaseFileBizDto = new TmptBaseFileBizDto();
        tmptBaseFileBizDto.setId(0L);
        if (id!=0) {
            TmptBaseFileBiz tmptBaseFileBiz = tmptBaseFileBizService.getById(id);
            tmptBaseFileBizDto = mapper.map(tmptBaseFileBiz, TmptBaseFileBizDto.class);
        }
        treeReduce(tmptBaseFileBizDto);
        result.setObj(tmptBaseFileBizDto);

        return result.success();
    }

    private void treeReduce(TmptBaseFileBizDto tmptBaseFileBizDto){
    }

    protected Result myPage(TmptBaseFileBizDto tmptBaseFileBizDto, UserDetailsDto userDetailsDto){
        Result<TmptBaseFileBizDto> result = new Result<>();

        Page<TmptBaseFileBiz> page = new Page<>(tmptBaseFileBizDto.getPage(), tmptBaseFileBizDto.getLimit());

        // 查询条件
        TmptBaseFileBiz tmptBaseFileBizQuery = mapper.map(tmptBaseFileBizDto, TmptBaseFileBiz.class);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        tmptBaseFileBizQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        tmptBaseFileBizQuery.rebuildDataLimitQueryBean(userDetailsDto);

        // 多表查询时，plus自带不支持，这里封装一个工具类处理，mapper.xml中主表别名是t1, 给所有非空字段加别名； 特殊查询条件，在工具类中要忽略掉，最后自定义增加。
        QueryWrapper<TmptBaseFileBiz> queryWrapper = MyQueryWrapper.buildQueryWrapper(tmptBaseFileBizQuery , "createdate");
        //特殊查询条件
        queryWrapper.ge(tmptBaseFileBizDto.getCreatedate_Start()!=null,"t1.createdate",tmptBaseFileBizDto.getCreatedate_Start());
        queryWrapper.le(tmptBaseFileBizDto.getCreatedate_End()!=null,"t1.createdate",tmptBaseFileBizDto.getCreatedate_End());
        queryWrapper.orderBy(tmptBaseFileBizDto.getCondition(), tmptBaseFileBizDto.isAsc(), "t1."+tmptBaseFileBizDto.getColumn());

        result = tmptBaseFileBizService.myPage(page, queryWrapper);

        return result;
    }

    protected Result selectBy(TmptBaseFileBizDto tmptBaseFileBizDto, UserDetailsDto userDetailsDto){
        Result<TmptBaseFileBizDto> result = new Result<>();

        // 查询条件
        TmptBaseFileBiz tmptBaseFileBizQuery = mapper.map(tmptBaseFileBizDto, TmptBaseFileBiz.class);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        tmptBaseFileBizQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        tmptBaseFileBizQuery.rebuildDataLimitQueryBean(userDetailsDto);

        QueryWrapper<TmptBaseFileBiz> queryWrapper=new QueryWrapper<>(tmptBaseFileBizQuery);
        //特殊查询条件
        queryWrapper.ge(tmptBaseFileBizDto.getCreatedate_Start()!=null,"createdate",tmptBaseFileBizDto.getCreatedate_Start());
        queryWrapper.le(tmptBaseFileBizDto.getCreatedate_End()!=null,"createdate",tmptBaseFileBizDto.getCreatedate_End());
        queryWrapper.orderBy(tmptBaseFileBizDto.getCondition(), tmptBaseFileBizDto.isAsc(), tmptBaseFileBizDto.getColumn());

        result = tmptBaseFileBizService.selectBy(queryWrapper);

        return result;
    }

    protected Result selectById(Long id){
        //调用服务
        return tmptBaseFileBizService.selectById(id);
    }

    protected Result select4column(String columnName){
        try {
            // vue默认传过来的，出于枚举类名考虑，首字母是大写，这样要小字当属性用。
            columnName = columnName.substring(0,1).toLowerCase()+columnName.substring(1);
            Object o = SpringContextHolder.getBean("tmptBaseFileService");
            Method method = o.getClass().getMethod("mySelect4"+columnName);
            return Result.builder(method.invoke(o)).success();
        }catch (Exception e){
            throw new RuntimeException("方法名不存在");
        }
    }

    protected Result create(@RequestBody TmptBaseFileBizDto tmptBaseFileBizDto,
                    String _userId, String _userName, String _companyId){
        //插入参数
        tmptBaseFileBizDto.setCreatemanId(Long.parseLong(_userId));
        tmptBaseFileBizDto.setCreateman(_userName);
        tmptBaseFileBizDto.setCreatedate(DateTool.getCurrentDatetimeStr());
        tmptBaseFileBizDto.setCompanyId(Long.parseLong(_companyId));

        //调用服务
        return tmptBaseFileBizService.create(tmptBaseFileBizDto);
    }

    protected Result update(@RequestBody TmptBaseFileBizDto tmptBaseFileBizDto,
                        String _userId, String _userName){
        //参数
        tmptBaseFileBizDto.setModifymanId(Long.parseLong(_userId));
        tmptBaseFileBizDto.setModifyman(_userName);
        tmptBaseFileBizDto.setModifydate(DateTool.getCurrentDatetimeStr());
        tmptBaseFileBizDto.setCreatedate(null);
        tmptBaseFileBizDto.setCreateman(null);
        tmptBaseFileBizDto.setCreatemanId(null);
        tmptBaseFileBizDto.setCompanyId(null);

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", tmptBaseFileBizDto.getId()==null?"0":tmptBaseFileBizDto.getId());

        //调用服务
        return tmptBaseFileBizService.update(tmptBaseFileBizDto, updateWrapper);
    }

    protected Result delete(Long id, String _userId, String _userName){
        Result result = new Result();

        //参数
        TmptBaseFileBizDto tmptBaseFileBizDto = new TmptBaseFileBizDto();
        tmptBaseFileBizDto.setModifymanId(Long.parseLong(_userId));
        tmptBaseFileBizDto.setModifyman(_userName);
        tmptBaseFileBizDto.setModifydate(DateTool.getCurrentDatetimeStr());
        tmptBaseFileBizDto.setDeleteflag("1");

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id==null?0:id.longValue());

        //调用服务
        return tmptBaseFileBizService.update(tmptBaseFileBizDto, updateWrapper);
    }

    protected Result deletePhysical(Long id){
        Result result = new Result();

        //调用服务
        Boolean aBoolean =  false;//tmptBaseFileBizService.removeById(id==null?0:id.longValue());
        if(aBoolean){
            result.success();
        }else {
            result.fault();
        }

        return result;
    }

}
